package com.sitech.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class LoginHandlerInterceptor extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		String loginUrl = "/login/login.do";
		String registerUrl = "/login/register.do";
		String Url = "/login/startLogin.html";
		/**
		 * 登录拦截
		 * */
		if(request.getServletPath().startsWith(Url)||request.getServletPath().startsWith(registerUrl )||request.getServletPath().startsWith(loginUrl )){
			return true;
		}
		if(request.getSession().getAttribute("username")!=null){
			return true;
		};
	    //重定向到登录页面  
		System.out.println("重定向");
	    response.sendRedirect(request.getContextPath() + Url);
	    return false;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		super.postHandle(request, response, handler, modelAndView);
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		super.afterCompletion(request, response, handler, ex);
	}
	
	
}
